home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / hAWK project / AWK Source / DEBUG.C < prev    next >
Text File  |  1994-12-22  |  11KB  |  569 lines

  1. /*
  2.  * debug.c -- Various debugging routines 
  3.  */
  4. /* At present, the Mac version does not support the DEBUG option. This file is still in the original
  5. unix/MSDOS form.
  6. */
  7. /* 
  8.  * Copyright (C) 1986, 1988, 1989 the Free Software Foundation, Inc.
  9.  * 
  10.  * This file is part of GAWK, the GNU implementation of the
  11.  * AWK Progamming Language.
  12.  * 
  13.  * GAWK is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 1, or (at your option)
  16.  * any later version.
  17.  * 
  18.  * GAWK is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  * 
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with GAWK; see the file COPYING.  If not, write to
  25.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  */
  27. /************NOTE WARNING OHGOSH LOOKOUT***************
  28.  
  29. This is the original unix/IBM file, NOT MODIFIED FOR MACINTOSH.
  30. It is not, in its present form, suitable for inclusion in hAWK-as-a-code-resource.
  31.  
  32. *************************************************/
  33. #include "AWK.H"
  34.  
  35. #ifdef DEBUG
  36.  
  37. extern NODE **fields_arr;
  38.  
  39.  
  40. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  41.  
  42. /*
  43.  * Some of it could be turned into a really cute trace command, if anyone
  44.  * wants to.  
  45.  */
  46. char *nnames[] = {
  47.     "illegal", "times", "quotient", "mod", "plus",
  48.     "minus", "cond_pair", "subscript", "concat", "exp",
  49.     /* 10 */
  50.     "preincrement", "predecrement", "postincrement", "postdecrement",
  51.     "unary_minus",
  52.     "field_spec", "assign", "assign_times", "assign_quotient", "assign_mod",
  53.     /* 20 */
  54.     "assign_plus", "assign_minus", "assign_exp", "and", "or",
  55.     "equal", "notequal", "less", "greater", "leq",
  56.     /* 30 */
  57.     "geq", "match", "nomatch", "not", "rule_list",
  58.     "rule_node", "statement_list", "if_branches", "expression_list",
  59.     "param_list",
  60.     /* 40 */
  61.     "K_if", "K_while", "K_for", "K_arrayfor", "K_break",
  62.     "K_continue", "K_print", "K_printf", "K_next", "K_exit",
  63.     /* 50 */
  64.     "K_do", "K_return", "K_delete", "K_getline", "K_function",
  65.     "redirect_output", "redirect_append", "redirect_pipe",
  66.     "redirect_pipein", "redirect_input",
  67.     /* 60 */
  68.     "var", "var_array", "val", "builtin", "line_range",
  69.     "in_array", "func", "func_call", "cond_exp", "regex",
  70.     /* 70 */
  71.     "hashnode", "ahash"
  72. };
  73.  
  74. ptree(n)
  75. NODE *n;
  76. {
  77.     print_parse_tree(n);
  78. }
  79.  
  80. pt()
  81. {
  82.     long x;
  83.  
  84.     (void) scanf("%x", &x);
  85.     printf("0x%x\n", x);
  86.     print_parse_tree((NODE *) x);
  87.     fflush(stdout);
  88. }
  89.  
  90. static depth = 0;
  91.  
  92. print_parse_tree(ptr)
  93. NODE *ptr;
  94. {
  95.     if (!ptr) {
  96.         printf("NULL\n");
  97.         return;
  98.     }
  99.     if ((short) (ptr->type) < 0 || (short) (ptr->type) > sizeof(nnames) / sizeof(nnames[0])) {
  100.         printf("(0x%x Type %d??)\n", ptr, ptr->type);
  101.         return;
  102.     }
  103.     printf("(%d)%*s", depth, depth, "");
  104.     switch ((short) ptr->type) {
  105.     case (short) Node_val:
  106.         printf("(0x%x Value ", ptr);
  107.         if (ptr->flags&STR)
  108.             printf("str: \"%.*s\" ", ptr->stlen, ptr->stptr);
  109.         if (ptr->flags&NUM)
  110.             printf("num: %g", ptr->numbr);
  111.         printf(")\n");
  112.         return;
  113.     case (short) Node_var_array:
  114.         {
  115.         struct search *l;
  116.  
  117.         printf("(0x%x Array)\n", ptr);
  118.         for (l = assoc_scan(ptr); l; l = assoc_next(l)) {
  119.             printf("\tindex: ");
  120.             print_parse_tree(l->retval);
  121.             printf("\tvalue: ");
  122.             print_parse_tree(*assoc_lookup(ptr, l->retval));
  123.             printf("\n");
  124.         }
  125.         return;
  126.         }
  127.     case Node_param_list:
  128.         printf("(0x%x Local variable %s)\n", ptr, ptr->param);
  129.         if (ptr->rnode)
  130.             print_parse_tree(ptr->rnode);
  131.         return;
  132.     case Node_regex:
  133.         printf("(0x%x Regular expression %s\n", ptr, ptr->re_text);
  134.         return;
  135.     }
  136.     if (ptr->lnode)
  137.         printf("0x%x = left<--", ptr->lnode);
  138.     printf("(0x%x %s.%d)", ptr, nnames[(short) (ptr->type)], ptr->type);
  139.     if (ptr->rnode)
  140.         printf("-->right = 0x%x", ptr->rnode);
  141.     printf("\n");
  142.     depth++;
  143.     if (ptr->lnode)
  144.         print_parse_tree(ptr->lnode);
  145.     switch ((short) ptr->type) {
  146.     case (short) Node_line_range:
  147.     case (short) Node_match:
  148.     case (short) Node_nomatch:
  149.         break;
  150.     case (short) Node_builtin:
  151.         printf("Builtin: %d\n", ptr->proc);
  152.         break;
  153.     case (short) Node_K_for:
  154.     case (short) Node_K_arrayfor:
  155.         printf("(%s:)\n", nnames[(short) (ptr->type)]);
  156.         print_parse_tree(ptr->forloop->init);
  157.         printf("looping:\n");
  158.         print_parse_tree(ptr->forloop->cond);
  159.         printf("doing:\n");
  160.         print_parse_tree(ptr->forloop->incr);
  161.         break;
  162.     default:
  163.         if (ptr->rnode)
  164.             print_parse_tree(ptr->rnode);
  165.         break;
  166.     }
  167.     --depth;
  168. }
  169.  
  170.  
  171. /*
  172.  * print out all the variables in the world 
  173.  */
  174.  
  175. dump_vars()
  176. {
  177.     register short n;
  178.     register NODE *buc;
  179.  
  180. #ifdef notdef
  181.     printf("Fields:");
  182.     dump_fields();
  183. #endif
  184.     printf("Vars:\n");
  185.     for (n = 0; n < HASHSIZE; n++) {
  186.         for (buc = variables[n]; buc; buc = buc->hnext) {
  187.             printf("'%.*s': ", buc->hlength, buc->hname);
  188.             print_parse_tree(buc->hvalue);
  189.         }
  190.     }
  191.     printf("End\n");
  192. }
  193.  
  194. #ifdef notdef
  195. dump_fields()
  196. {
  197.     register NODE **p;
  198.     register short n;
  199.  
  200.     printf("%d fields\n", f_arr_siz);
  201.     for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++) {
  202.         printf("$%d is '", n);
  203.         print_simple(*p, stdout);
  204.         printf("'\n");
  205.     }
  206. }
  207. #endif
  208.  
  209. /* VARARGS1 */
  210. print_debug(str, n)
  211. char *str;
  212. {
  213.     extern short debugging;
  214.  
  215.     if (debugging)
  216.         printf("%s:0x%x\n", str, n);
  217. }
  218.  
  219. short indent = 0;
  220.  
  221. print_a_node(ptr)
  222. NODE *ptr;
  223. {
  224.     NODE *p1;
  225.     char *str, *str2;
  226.     short n;
  227.     NODE *buc;
  228.  
  229.     if (!ptr)
  230.         return;        /* don't print null ptrs */
  231.     switch (ptr->type) {
  232.     case Node_val:
  233.         if (ptr->flags&NUM)
  234.             printf("%g", ptr->numbr);
  235.         else
  236.             printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  237.         return;
  238.     case Node_times:
  239.         str = "*";
  240.         goto pr_twoop;
  241.     case Node_quotient:
  242.         str = "/";
  243.         goto pr_twoop;
  244.     case Node_mod:
  245.         str = "%";
  246.         goto pr_twoop;
  247.     case Node_plus:
  248.         str = "+";
  249.         goto pr_twoop;
  250.     case Node_minus:
  251.         str = "-";
  252.         goto pr_twoop;
  253.     case Node_exp:
  254.         str = "^";
  255.         goto pr_twoop;
  256.     case Node_concat:
  257.         str = " ";
  258.         goto pr_twoop;
  259.     case Node_assign:
  260.         str = "=";
  261.         goto pr_twoop;
  262.     case Node_assign_times:
  263.         str = "*=";
  264.         goto pr_twoop;
  265.     case Node_assign_quotient:
  266.         str = "/=";
  267.         goto pr_twoop;
  268.     case Node_assign_mod:
  269.         str = "%=";
  270.         goto pr_twoop;
  271.     case Node_assign_plus:
  272.         str = "+=";
  273.         goto pr_twoop;
  274.     case Node_assign_minus:
  275.         str = "-=";
  276.         goto pr_twoop;
  277.     case Node_assign_exp:
  278.         str = "^=";
  279.         goto pr_twoop;
  280.     case Node_and:
  281.         str = "&&";
  282.         goto pr_twoop;
  283.     case Node_or:
  284.         str = "||";
  285.         goto pr_twoop;
  286.     case Node_equal:
  287.         str = "==";
  288.         goto pr_twoop;
  289.     case Node_notequal:
  290.         str = "!=";
  291.         goto pr_twoop;
  292.     case Node_less:
  293.         str = "<";
  294.         goto pr_twoop;
  295.     case Node_greater:
  296.         str = ">";
  297.         goto pr_twoop;
  298.     case Node_leq:
  299.         str = "<=";
  300.         goto pr_twoop;
  301.     case Node_geq:
  302.         str = ">=";
  303.         goto pr_twoop;
  304.  
  305. pr_twoop:
  306.         print_a_node(ptr->lnode);
  307.         printf("%s", str);
  308.         print_a_node(ptr->rnode);
  309.         return;
  310.  
  311.     case Node_not:
  312.         str = "!";
  313.         str2 = "";
  314.         goto pr_oneop;
  315.     case Node_field_spec:
  316.         str = "$(";
  317.         str2 = ")";
  318.         goto pr_oneop;
  319.     case Node_postincrement:
  320.         str = "";
  321.         str2 = "++";
  322.         goto pr_oneop;
  323.     case Node_postdecrement:
  324.         str = "";
  325.         str2 = "--";
  326.         goto pr_oneop;
  327.     case Node_preincrement:
  328.         str = "++";
  329.         str2 = "";
  330.         goto pr_oneop;
  331.     case Node_predecrement:
  332.         str = "--";
  333.         str2 = "";
  334.         goto pr_oneop;
  335. pr_oneop:
  336.         printf(str);
  337.         print_a_node(ptr->subnode);
  338.         printf(str2);
  339.         return;
  340.  
  341.     case Node_expression_list:
  342.         print_a_node(ptr->lnode);
  343.         if (ptr->rnode) {
  344.             printf(",");
  345.             print_a_node(ptr->rnode);
  346.         }
  347.         return;
  348.  
  349.     case Node_var:
  350.         for (n = 0; n < HASHSIZE; n++) {
  351.             for (buc = variables[n]; buc; buc = buc->hnext) {
  352.                 if (buc->hvalue == ptr) {
  353.                     printf("%.*s", buc->hlength, buc->hname);
  354.                     n = HASHSIZE;
  355.                     break;
  356.                 }
  357.             }
  358.         }
  359.         return;
  360.     case Node_subscript:
  361.         print_a_node(ptr->lnode);
  362.         printf("[");
  363.         print_a_node(ptr->rnode);
  364.         printf("]");
  365.         return;
  366.     case Node_builtin:
  367.         printf("some_builtin(");
  368.         print_a_node(ptr->subnode);
  369.         printf(")");
  370.         return;
  371.  
  372.     case Node_statement_list:
  373.         printf("{\n");
  374.         indent++;
  375.         for (n = indent; n; --n)
  376.             printf("  ");
  377.         while (ptr) {
  378.             print_maybe_semi(ptr->lnode);
  379.             if (ptr->rnode)
  380.                 for (n = indent; n; --n)
  381.                     printf("  ");
  382.             ptr = ptr->rnode;
  383.         }
  384.         --indent;
  385.         for (n = indent; n; --n)
  386.             printf("  ");
  387.         printf("}\n");
  388.         for (n = indent; n; --n)
  389.             printf("  ");
  390.         return;
  391.  
  392.     case Node_K_if:
  393.         printf("if(");
  394.         print_a_node(ptr->lnode);
  395.         printf(") ");
  396.         ptr = ptr->rnode;
  397.         if (ptr->lnode->type == Node_statement_list) {
  398.             printf("{\n");
  399.             indent++;
  400.             for (p1 = ptr->lnode; p1; p1 = p1->rnode) {
  401.                 for (n = indent; n; --n)
  402.                     printf("  ");
  403.                 print_maybe_semi(p1->lnode);
  404.             }
  405.             --indent;
  406.             for (n = indent; n; --n)
  407.                 printf("  ");
  408.             if (ptr->rnode) {
  409.                 printf("} else ");
  410.             } else {
  411.                 printf("}\n");
  412.                 return;
  413.             }
  414.         } else {
  415.             print_maybe_semi(ptr->lnode);
  416.             if (ptr->rnode) {
  417.                 for (n = indent; n; --n)
  418.                     printf("  ");
  419.                 printf("else ");
  420.             } else
  421.                 return;
  422.         }
  423.         if (!ptr->rnode)
  424.             return;
  425.         deal_with_curls(ptr->rnode);
  426.         return;
  427.  
  428.     case Node_K_while:
  429.         printf("while(");
  430.         print_a_node(ptr->lnode);
  431.         printf(") ");
  432.         deal_with_curls(ptr->rnode);
  433.         return;
  434.  
  435.     case Node_K_do:
  436.         printf("do ");
  437.         deal_with_curls(ptr->rnode);
  438.         printf("while(");
  439.         print_a_node(ptr->lnode);
  440.         printf(") ");
  441.         return;
  442.  
  443.     case Node_K_for:
  444.         printf("for(");
  445.         print_a_node(ptr->forloop->init);
  446.         printf(";");
  447.         print_a_node(ptr->forloop->cond);
  448.         printf(";");
  449.         print_a_node(ptr->forloop->incr);
  450.         printf(") ");
  451.         deal_with_curls(ptr->forsub);
  452.         return;
  453.     case Node_K_arrayfor:
  454.         printf("for(");
  455.         print_a_node(ptr->forloop->init);
  456.         printf(" in ");
  457.         print_a_node(ptr->forloop->incr);
  458.         printf(") ");
  459.         deal_with_curls(ptr->forsub);
  460.         return;
  461.  
  462.     case Node_K_printf:
  463.         printf("printf(");
  464.         print_a_node(ptr->lnode);
  465.         printf(")");
  466.         return;
  467.     case Node_K_print:
  468.         printf("print(");
  469.         print_a_node(ptr->lnode);
  470.         printf(")");
  471.         return;
  472.     case Node_K_next:
  473.         printf("next");
  474.         return;
  475.     case Node_K_break:
  476.         printf("break");
  477.         return;
  478.     case Node_K_delete:
  479.         printf("delete ");
  480.         print_a_node(ptr->lnode);
  481.         return;
  482.     case Node_func:
  483.         printf("function %s (", ptr->lnode->param);
  484.         if (ptr->lnode->rnode)
  485.             print_a_node(ptr->lnode->rnode);
  486.         printf(")\n");
  487.         print_a_node(ptr->rnode);
  488.         return;
  489.     case Node_param_list:
  490.         printf("%s", ptr->param);
  491.         if (ptr->rnode) {
  492.             printf(", ");
  493.             print_a_node(ptr->rnode);
  494.         }
  495.         return;
  496.     default:
  497.         print_parse_tree(ptr);
  498.         return;
  499.     }
  500. }
  501.  
  502. print_maybe_semi(ptr)
  503. NODE *ptr;
  504. {
  505.     print_a_node(ptr);
  506.     switch (ptr->type) {
  507.     case Node_K_if:
  508.     case Node_K_for:
  509.     case Node_K_arrayfor:
  510.     case Node_statement_list:
  511.         break;
  512.     default:
  513.         printf(";\n");
  514.         break;
  515.     }
  516. }
  517.  
  518. deal_with_curls(ptr)
  519. NODE *ptr;
  520. {
  521.     short n;
  522.  
  523.     if (ptr->type == Node_statement_list) {
  524.         printf("{\n");
  525.         indent++;
  526.         while (ptr) {
  527.             for (n = indent; n; --n)
  528.                 printf("  ");
  529.             print_maybe_semi(ptr->lnode);
  530.             ptr = ptr->rnode;
  531.         }
  532.         --indent;
  533.         for (n = indent; n; --n)
  534.             printf("  ");
  535.         printf("}\n");
  536.     } else {
  537.         print_maybe_semi(ptr);
  538.     }
  539. }
  540.  
  541. NODE *
  542. do_prvars()
  543. {
  544.     dump_vars();
  545.     return Nnull_string;
  546. }
  547.  
  548. NODE *
  549. do_bp()
  550. {
  551.     return Nnull_string;
  552. }
  553.  
  554. #endif
  555.  
  556. #ifdef MEMDEBUG
  557.  
  558. #undef free
  559. extern void free();
  560.  
  561. void
  562. do_free(s)
  563. char *s;
  564. {
  565.     free(s);
  566. }
  567.  
  568. #endif
  569.